package com.example.easypoi.demo3.contorller;

import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.ExcelImportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import cn.afterturn.easypoi.excel.entity.ImportParams;
import cn.afterturn.easypoi.excel.entity.enmus.ExcelType;
import com.example.easypoi.demo3.entity.SimpleUserPicture;
import com.example.easypoi.demo3.handler.ExcelDictAddressListHandlerImpl;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.IOUtils;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.util.ResourceUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.URLEncoder;
import java.nio.file.Files;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;


@RestController
@Slf4j
public class SimpleUserPictureController {
    private static void getSimpleUserPicture(List<SimpleUserPicture> simpleUserPictures) throws IOException {
        SimpleUserPicture simpleUserPicture = new SimpleUserPicture();
        simpleUserPicture.setRealName("张三");
        simpleUserPicture.setBirthdayStr("2024:12:01 08:25:30");
        simpleUserPicture.setBirthday(LocalDateTime.now());
        simpleUserPicture.setSexReplacePullDownList(1);
        simpleUserPicture.setSexDictPullDownList(0);
        simpleUserPicture.setAvatarLocalFile("C:\\Users\\wywex\\IdeaProjects\\easypoi-demo\\src\\main\\resources\\picture\\picture1.jpg");
        File picture = ResourceUtils.getFile("classpath:picture/picture2.jpg");
        InputStream pictureStream = Files.newInputStream(picture.toPath());
        byte[] bytes = IOUtils.toByteArray(pictureStream);
        simpleUserPicture.setAvatarByteArray(bytes);
        simpleUserPictures.add(simpleUserPicture);
    }

    @GetMapping("/simpleUserPicture")
    public void getUserPicture(HttpServletResponse response) throws IOException {
        ExportParams exportParams = new ExportParams();
        exportParams.setDictHandler(new ExcelDictAddressListHandlerImpl());
//        exportParams.setType(ExcelType.XSSF);
        exportParams.setType(ExcelType.HSSF);
        exportParams.setSheetName("用户信息");
        exportParams.setTitle("用户信息");
        exportParams.setTitleHeight((short) 10);
        exportParams.setHeight((short) 8);
        ArrayList<SimpleUserPicture> simpleUserPictures = new ArrayList<>();
        getSimpleUserPicture(simpleUserPictures);
        try (Workbook workbook = ExcelExportUtil.exportExcel(exportParams, SimpleUserPicture.class, simpleUserPictures)) {
            response.reset();
            response.setCharacterEncoding("UTF-8");
            response.setHeader("content-Type", "application/vnd.ms-excel");
            String suffix = exportParams.getType() == ExcelType.XSSF ? ".xlsx" : ".xls";
            response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder
                    .encode("简单图片导出示例" + suffix, "UTF-8"));//这里根据上面ExcelType.XSSF配置来配，如果是 ExcelType.XSSF 对应 .xlsx ExcelType.HSSF对应.xls
            workbook.write(response.getOutputStream());
        } catch (Exception e) {
            log.error("error {}", e.getMessage());
            throw new IOException(e.getMessage());
        }
    }

    @PostMapping("/importSimpleUser")
    public Boolean importSimpleUser(@RequestPart("file") MultipartFile multipartFile) throws Exception {
        ImportParams importParams = new ImportParams();
        importParams.setHeadRows(1);
        importParams.setTitleRows(1);
        importParams.setDictHandler(new ExcelDictAddressListHandlerImpl());
        List<SimpleUserPicture> simpleUserPictures = ExcelImportUtil.importExcel(multipartFile.getInputStream(), SimpleUserPicture.class, importParams);
        for (SimpleUserPicture simpleUserPicture : simpleUserPictures) {
            log.info("{}", simpleUserPicture);
        }
        return true;
    }
}

